package com.logic.landseaserver.ws;

import java.util.HashMap;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.logic.landseaserver.common.LandeaConstants;
import com.logic.landseaserver.ws.request.AddAuthCodeReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.domain.OffRent;
import com.logic.landseaserver.domain.OffrentConfirm;
import com.logic.landseaserver.models.prepayment.service.AutoPayBillService;
import com.logic.landseaserver.service.IOffRent;
import com.logic.landseaserver.ws.dto.OffRentContractDTO;
import com.logic.landseaserver.ws.dto.OffrentConfirmDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 *
 * 项目名称：system-server</br>
 * 类名称：OffRentRest</br>
 * 类描述：</br>
 * 创建人：tony</br>
 * 创建时间：2017年8月14日 下午2:04:32</br>
 * @version 1.0
 *
 */
@Path("/offRent")
@Api(value = "offRent", description = "退租 Rest WebServices", position = 42)
public class OffRentRest {
    
    private static final Logger logger = LoggerFactory.getLogger(OffRentRest.class);
    
    @Autowired
    private IOffRent offRentServiceImpl;
    
    @Autowired
    private AutoPayBillService autoPayBillService;
    
    @POST
    @Path("/applyOffRent")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "退租申请(保存 & 编辑)")
    public Response applyOffRent(
            @ApiParam(value = "param", required = false) OffRent offRentrequest) {
        CommonResultMap map = new CommonResultMap();
        try {
            HashMap<String,Object> result = offRentServiceImpl.addOrEditOffrent(offRentrequest);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e){
            LoggerHelper.handleException(logger, "applyOffRent", e, map);
        }
        catch (Exception e) {
            logger.error("offRent || applyOffRent error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/getOffRentByContractId/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据合同id查询退租申请")
    public Response getOffRentByContractId(
        @ApiParam(value = "param", required = true) @PathParam("contractId") Integer contractId){
        CommonResultMap map = new CommonResultMap();
        try {
            OffRentContractDTO offRentcontractDTO = offRentServiceImpl.getOffRent(contractId,null);
            map.setData(offRentcontractDTO);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("offRent || getOffRentByContractId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @DELETE
    @Path("/cancelApply/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "取消退房申请记录")
    public Response cancelApply(
        @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId){
        CommonResultMap map = new CommonResultMap();
        try {
            HashMap<String,Object> result = offRentServiceImpl.cancelApplyByContractId(contractId);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            logger.error("offRent || getOffRentByContractId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        } catch (Exception e) {
            logger.error("offRent || getOffRentByContractId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/createPreBill/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "生成预结算单")
    public Response createPreBill(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
           offRentServiceImpl.addPreBill(contractId);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e) {
            logger.error("offRent || createPreBill error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("offRent || createPreBill error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/queryPreBill/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取预结算单")
    public Response queryPreBill(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData( offRentServiceImpl.queryPreBill(contractId, LandeaConstants.OffRentBillType.PREBILL));
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            logger.error("offRent || queryPreBill LandseaException error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        }  catch (Exception e) {
            logger.error("offRent || createPreBill error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/queryOffRentBill/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取结算单")
    public Response queryOffRentBill(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData( offRentServiceImpl.queryPreBill(contractId,LandeaConstants.OffRentBillType.OFFRENTBILL));
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            logger.error("offRent || queryPreBill LandseaException error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        }  catch (Exception e) {
            logger.error("offRent || createPreBill error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/createOffRentBill/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "生成退房账单")
    public Response createOffRentBill(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            offRentServiceImpl.createOffRentBill(contractId);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            logger.error("offRent || createOffRentBill error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("offRent || createOffRentBill error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/uploadOffrentConfirm")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "保存退租确认")
    public Response uploadOffrentConfirm(
            @ApiParam(value = "param", required = true) OffrentConfirm offrentConfirm) {
        CommonResultMap map = new CommonResultMap();
        try {
            HashMap<String,Object> result = offRentServiceImpl.uploadOffrentConfirm(offrentConfirm);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e) {
            logger.error("offRent || uploadOffrentConfirm error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("offRent || applyOffRent error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/getOffrentConfirm/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询退租确认")
    public Response getOffrentConfirm(
            @ApiParam(value = "param", required = true) @PathParam(value="contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            OffrentConfirmDTO oc = offRentServiceImpl.getOffrentConfirm(contractId);
            map.setData(oc);
            map.setResult(CommonResult.SUCCESS);
        }catch (BusinessException e) {
            logger.error("offRent || getOffrentConfirm error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        } catch (Exception e) {
            logger.error("offRent || applyOffRent error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }



    @DELETE
    @Path("/cancelOffRent/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "取消退房")
    public Response cancelOffRent(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId){
        CommonResultMap map = new CommonResultMap();
        try {
            HashMap<String,Object> result = new HashMap<>();
            result.put("result", offRentServiceImpl.cancelOffRent(contractId));
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            logger.error("offRent || getOffRentByContractId error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("offRent || getOffRentByContractId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/addAuthCode")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "保存优惠劵使用记录->退房账单")
    public Response usedAuthCode(
            @ApiParam(value = "param", required = true) AddAuthCodeReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            offRentServiceImpl.addAuthCode(req);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e) {
            logger.error("offRent || addAuthCode error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("offRent || addAuthCode error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

}


